describe('Service: geo', function () {

  // load the service's module
  beforeEach(module('webappApp'));

  // instantiate service
  var geo, geolocation;
  beforeEach(module(function ($provide) {
    $provide.factory('geolocation', function () {
      return jasmine.createSpy();
    });
  }));

  beforeEach(inject(function($injector) {
    geo = $injector.get('geo');
    geolocation = $injector.get('geolocation');
  }));

  it('should calcuate the center of a map/list of location-having objects', function () {
    var hasLocs = [{
      location: {
        latitude: 1,
        longitude: 1
      }
    }, {
      location: {
        latitude: -1,
        longitude: -1
      }
    }];
    expect(geo.center(hasLocs).latitude).toBe(0);
    expect(geo.center(hasLocs).longitude).toBe(0);
  });
    
  it('should calculate the bounds of a map/array of location-having objects', function () {
    var hasLocs = {
      1: {
        location: {
          latitude: -10,
          longitude: -5
        }
      },
      2: {
        location: {
          latitude: 10,
          longitude: 5
        }
      }
    },
    bounds = geo.bounds(hasLocs),
    ne = bounds.getNorthEast(),
    sw = bounds.getSouthWest();
    expect(ne.lat()).toBe(10);
    expect(ne.lng()).toBe(5);
    expect(sw.lat()).toBe(-10);
    expect(sw.lng()).toBe(-5);
  });

  it('should default to the whole world on null or undefined bounds input', function () {
    var bounds = geo.bounds(),
    ne = bounds.getNorthEast(),
    sw = bounds.getSouthWest();
    expect(ne.lat()).toBe(90);
    expect(ne.lng()).toBe(180);
    expect(sw.lat()).toBe(-90);
    expect(sw.lng()).toBe(-180);
  });

  it('should return the user\'s location as a promise', function () {
    geolocation.getCurrentPosition = function (callback) {
      callback({
        coords: {
          latitude: 5,
          longitude: 5
        }
      });
    };
    var promise = geo.location().then(function (pos) {
      expect(pos.latitude).toBe(5);
      expect(pos.longitude).toBe(5);
    }, function () {
      // should never call
      expect(false).toBeTruthy();
    });
  });

  it('should return a rejected promise if geolocation is not supported', function () {
    geolocation.getCurrentPosition = function (callback, failure) {
      failure('failure');
    };
    var promise = geo.location().then(function () {
      expect(false).toBeTruthy();
    }, function (val) {
      expect(val).toBeDefined();
    });
  });
});
